package com.hhkj.study.admin.user.controller;

import com.hhkj.study.admin.user.model.req.UserCourseStatisticReq;
import com.hhkj.study.admin.user.model.req.UserReq;
import com.hhkj.study.admin.user.model.req.UserStatisticExportReq;
import com.hhkj.study.admin.user.service.biz.UserStatisticBizService;
import com.ruoyi.common.annotation.PermissionPrefix;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.GlobalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
* UserController
*
* @author SERVER
* @since 2024-09-06
*/
@Slf4j
@RestController
@PermissionPrefix("user:statistic")
@RequestMapping("/admin/customer/user/statistic")
public class UserStatisticController {

    @Resource
    private UserStatisticBizService userStatisticBizService;

    /**
     * 导出用户数据
     * @param req
     * @param response
     */
//    @PreAuthorize("@ss.hasPermi(this, ':export')")
    @PostMapping("/export")
    public void export(@RequestBody UserStatisticExportReq req, HttpServletResponse response) {
        try {
            userStatisticBizService.exportUserStatistic(req, response);
        } catch (GlobalException e) {
            throw new GlobalException(e.getMessage());
        } catch (Exception e) {
            log.error("导出文件失败", e);
            throw new GlobalException("系统异常，文件导出失败，请稍后重试");
        }
    }

    /**
     * 统计课程
     * @return
     */
    @PostMapping("/course")
    public TableDataInfo countUserCourse(@Validated @RequestBody UserCourseStatisticReq req){
        return TableDataInfo.buildSuccess(userStatisticBizService.countUserCourse(req));
    }

    /**
     * 统计听课情况
     * @return
     */
    @PostMapping("/listen")
    public TableDataInfo countUserListen(@Validated @RequestBody UserCourseStatisticReq req){
        return TableDataInfo.buildSuccess(userStatisticBizService.countUserCourse(req));
    }
}
